package com.misoo.ex07;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Ex07Activity extends Activity implements OnItemClickListener {
	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
	private String[] data = { " Item-1 ", " Item-2 ", " Item-3 " };
	private TextView tv;
	private String selection = "****";
	private ArrayAdapter<String> adapter;
	private boolean[] status = { false, false, false };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		RelativeLayout r_layout = new RelativeLayout(this);
		setContentView(r_layout);

		LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout layout = (LinearLayout) inflate.inflate(R.layout.list,
				null);

		RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
				175, WC);
		layout.setId(1);
		r_layout.addView(layout, param);

		tv = (TextView) layout.findViewById(R.id.text);
		ListView lv = (ListView) layout.findViewById(R.id.list);
		lv.setBackgroundResource(R.drawable.bk_red);

		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_multiple_choice, data);
		lv.setAdapter(adapter);
		lv.setItemsCanFocus(false);
		lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		lv.setOnItemClickListener(this);

		Button btn1 = new Button(this);
		btn1.setBackgroundResource(R.drawable.bk_ok_exit);
		btn1.setId(2);
		btn1.setText("OK");
		btn1.setOnClickListener(listener);
		param = new RelativeLayout.LayoutParams(60, WC);
		param.addRule(RelativeLayout.RIGHT_OF, 1);
		param.leftMargin = 15;
		param.topMargin = 20;
		r_layout.addView(btn1, param);

		Button btn2 = new Button(this);
		btn2.setBackgroundResource(R.drawable.bk_ok_exit);
		btn2.setId(3);
		btn2.setText("Exit");
		btn2.setOnClickListener(listener);
		param = new RelativeLayout.LayoutParams(60, WC);
		param.addRule(RelativeLayout.BELOW, 2);
		param.addRule(RelativeLayout.ALIGN_LEFT, 2);
		param.topMargin = 25;
		r_layout.addView(btn2, param);
	}

	OnClickListener listener = new OnClickListener() {
		public void onClick(View v) {
			if (v.getId() == 2) {
				String ss = "{";
				for (int i = 0; i < adapter.getCount(); i++) {
					if (status[i]) {
						ss += data[i];
						ss += " ";
					}
				}
				ss += "}";
				setTitle(ss);
			} else if (v.getId() == 3)
				finish();
		}
	};

	public void onItemClick(AdapterView<?> arg0, View v, int idx, long arg3) {
		status[idx] = !status[idx];
	}
}